//
//  HJJRadioDetailViewController.m
//  Pianke
//
//  Created by 黄俊杰 on 15/12/26.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "HJJRadioDetailViewController.h"
#import "HJJRadioDetailCell.h"
#import "HJJRadioRootModel.h"
#import "HJJPlayerViewController.h"

@interface HJJRadioDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UIImageView *topImage;

@property (nonatomic, strong)UIImageView *headImage;

@property (nonatomic, strong)UILabel *userName;

@property (nonatomic, strong)UILabel *contentLabel;

@property (nonatomic, strong)UIImageView *countImage;

@property (nonatomic, strong)UILabel *countLable;

@property (nonatomic, strong)UITableView *myTableView;

@property (nonatomic, strong)NSMutableArray *listArray;

@end

@implementation HJJRadioDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    self.title = self.naviTitle;
    self.listArray = [NSMutableArray array];
    
    //头View
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375 * EACH_WIDTH, 260 * EACH_HEIGHT)];
    
    self.topImage = [[UIImageView alloc] init];
    [topView addSubview:self.topImage];
    [self.topImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(self.topImage.mas_top).offset(170* EACH_HEIGHT);
    }];
    
    self.headImage = [[UIImageView alloc] init];
    self.headImage.layer.cornerRadius = 12;
    self.headImage.layer.masksToBounds = 1;
    [topView addSubview:self.headImage];
    [self.headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(15 * EACH_WIDTH);
        make.top.mas_equalTo(self.topImage.mas_bottom).offset(15* EACH_HEIGHT);
        make.right.mas_equalTo(self.headImage.mas_left).offset(24 * EACH_WIDTH);
        make.bottom.mas_equalTo(self.headImage.mas_top).offset(24* EACH_HEIGHT);
    }];
    
    self.userName = [[UILabel alloc] init];
    self.userName.font = [UIFont boldSystemFontOfSize:16];
    self.userName.textColor = GRAYCOLOR;
    [topView addSubview:self.userName];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.headImage.mas_right).offset(10 * EACH_WIDTH);
        make.right.mas_equalTo(self.userName.mas_left).offset(100 * EACH_WIDTH);
        make.top.mas_equalTo(self.headImage.mas_top).offset(3* EACH_HEIGHT);
        make.bottom.mas_equalTo(self.headImage.mas_bottom).offset(-3* EACH_HEIGHT);
    }];
    
    self.countImage = [[UIImageView alloc] init];
    self.countImage.image = [UIImage imageNamed:@"umeng_fb_audio_play_default@2x.png"];
    [topView addSubview:self.countImage];
    [self.countImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.userName.mas_right).offset(150 * EACH_WIDTH);
        make.right.mas_equalTo(self.countImage.mas_left).offset(10 * EACH_WIDTH);
        make.top.mas_equalTo(self.userName.mas_top).offset(2* EACH_HEIGHT);
        make.bottom.mas_equalTo(self.userName.mas_bottom).offset(-2* EACH_HEIGHT);
    }];
    
    self.countLable = [[UILabel alloc] init];
    self.countLable.font = [UIFont systemFontOfSize:11];
    self.countLable.textColor = BLUECOLOR;
    [topView addSubview:self.countLable];
    [self.countLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.countImage.mas_right).offset(5 * EACH_WIDTH);
        make.right.mas_equalTo(self.countLable.mas_left).offset(50 * EACH_WIDTH);
        make.top.bottom.equalTo(self.countImage);
    }];
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.font = [UIFont systemFontOfSize:14];
    self.contentLabel.textColor = GREENCOLOR;
    [topView addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.headImage);
        make.top.mas_equalTo(self.headImage.mas_bottom).offset(10* EACH_HEIGHT);
        make.bottom.mas_equalTo(self.contentLabel.mas_top).offset(30* EACH_HEIGHT);
        make.right.mas_equalTo(-15 * EACH_WIDTH);
    }];
    
    self.myTableView = [[UITableView alloc] init];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.myTableView.tableHeaderView = topView;
    [self.myTableView registerClass:[HJJRadioDetailCell class] forCellReuseIdentifier:@"HJJRadioDetailCell"];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.left.top.bottom.mas_equalTo(0);
    }];
    
    [self getData];
    
}

-(void)getData{

    NSString *url = @"http://api2.pianke.me/ting/radio_detail";
    NSString *body = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&radioid=%@&version=3.0.6", self.radioid];;
    NSDictionary *cookie = @{@"PHPSESSID":@"dlbvjmr0e59672q3lseu1fomu5"};
    [LDJ_AppTools postUrl:url body:body result:LDJJSON requestStyle:LDJRequestString headerFile:cookie success:^(id result) {
        
//        NSLog(@"+++++%@",result);
        NSMutableArray *list = [[result objectForKey:@"data"] objectForKey:@"list"];
        
        for (NSMutableDictionary *listDic in list) {
            HJJRadioRootModel *radio = [[HJJRadioRootModel alloc] init];
            [radio setValuesForKeysWithDictionary:listDic];
            radio.tingId = [listDic objectForKey:@"tingid"];
            radio.playSharetext = [[listDic objectForKey:@"playInfo"] objectForKey:@"sharetext"];
            radio.playShareurl = [[listDic objectForKey:@"playInfo"] objectForKey:@"shareurl"];
            radio.webview_url = [[listDic objectForKey:@"playInfo"] objectForKey:@"webview_url"];
            radio.uname = [[[listDic objectForKey:@"playInfo"] objectForKey:@"authorinfo"] objectForKey:@"uname"];
            
            [self.listArray addObject:radio];
        }
        
        //头视图赋值
        NSMutableDictionary *radioInfoDic = [[result objectForKey:@"data"] objectForKey:@"radioInfo"];
        
        NSURL *topUrl = [NSURL URLWithString:[radioInfoDic objectForKey:@"coverimg"]];
        [self.topImage sd_setImageWithURL:topUrl placeholderImage:nil];
        
        self.contentLabel.text = [radioInfoDic objectForKey:@"desc"];
        
        NSURL *headUrl = [NSURL URLWithString:[[radioInfoDic objectForKey:@"userinfo"] objectForKey:@"icon"]];
        [self.headImage sd_setImageWithURL:headUrl placeholderImage:nil];
        self.userName.text = [[radioInfoDic objectForKey:@"userinfo"] objectForKey:@"uname"];
        self.countLable.text = [NSString stringWithFormat:@"%ld",[[radioInfoDic objectForKey:@"musicvisitnum"]integerValue]];
        

        [self.myTableView reloadData];

    } failure:^(NSError *error) {
        
        
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId = @"HJJRadioDetailCell";
    HJJRadioDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    HJJRadioRootModel *model = [self.listArray objectAtIndex:indexPath.row];
    [cell sendModel:model];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [[HJJPlayerViewController sharePlayerViewController] setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [HJJPlayerViewController sharePlayerViewController].indexRow = indexPath.row;
    [HJJPlayerViewController sharePlayerViewController].listArray = self.listArray;
    [self presentViewController:[HJJPlayerViewController sharePlayerViewController] animated:1 completion:^{
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
